//
//  ZYJBaseViewController.m
//  TravelingByCar
//
//  Created by Wendale on 15-3-6.
//  Copyright (c) 2015年 ZYJ. All rights reserved.
//

#import "ICBaseViewController.h"
//#import "ICSettingViewController.h"
//#import "ICSearchViewController.h"
#import "iToast.h"
//#import "ICSegmentView.h"

@interface ICBaseViewController ()
@property(nonatomic, strong)UIImageView *emptyImgView;
@property(nonatomic, strong)UILabel *emptyLabel;
@property(nonatomic, strong)UIView *noNetWorkView;
@property(nonatomic, strong)UIButton *meesageBtn;
@property(nonatomic, strong)UIView *unReadView;

@end

@implementation ICBaseViewController

- (void)showHUD {
    [self.view bringSubviewToFront:self.HUD];
    [self.HUD show:YES];
}

- (void)showHUDWithMessage:(NSString *)message {
    [self.view bringSubviewToFront:self.HUD];
    self.HUD.labelText = message;
    [self.HUD show:YES];
}

- (void)showToast:(NSString *)message{
    [[iToast makeText:message] show];
}

#pragma mark -

- (void)loadView {
    [super loadView];
    self.view.backgroundColor = RGBCOLOR(0xf2f3f4);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:17.0];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = kBlack333;
    self.navigationItem.titleView = label;
    label.text = self.title;
    [label sizeToFit];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setHUD:[[MBProgressHUD alloc] initWithView:self.view]];
    self.navigationController.navigationBar.translucent = NO;
    [self.view addSubview:self.emptyImgView];
    [self.view addSubview:self.emptyLabel];
    [self.view addSubview:self.noNetWorkView];
}

- (void)initLeftMessageItem{
    UIImage *normal = IMG(@"nav_icon_back");
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:normal forState:UIControlStateNormal];
    leftButton.frame = ccr(0, 0, 50, 44);
    leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [leftButton addTarget:self action:@selector(goToMessage)
         forControlEvents:UIControlEventTouchUpInside];
//    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, normal.size.width/2.f - 5.f, 0, 0)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.meesageBtn = leftButton;
}
- (void)unReadNumCountUpdate{
    if (APP_CONTEXT.accountManager.messageUnreadInfo.unReadNum.integerValue == 0) {
        [self hideUnreadView];
    }else{
        [self showUnReadView];
    }
}
- (void)showUnReadView{
    self.unReadView.right = self.meesageBtn.width - 30.f;
    self.unReadView.top =  10.f;
    [self.meesageBtn addSubview:self.unReadView];
}

- (void)hideUnreadView{
    [self.unReadView removeFromSuperview];
}

- (void)initRightSearchItem{
    UIImage *normal = IMG(@"search");
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:normal forState:UIControlStateNormal];
    rightButton.frame = ccr(0, 0, 38, 38);
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [rightButton addTarget:self action:@selector(search:)
          forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImageEdgeInsets:UIEdgeInsetsMake(0, normal.size.width/2.f - 5.f, 0, 0)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)initRightSettingItem{
    UIImage *normal = IMG(@"setting");
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:normal forState:UIControlStateNormal];
    rightButton.frame = ccr(0, 0, 37, 36);
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [rightButton addTarget:self action:@selector(setting:)
         forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImageEdgeInsets:UIEdgeInsetsMake(0, normal.size.width/2.f - 5.f, 0, 0)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}


- (void)setHUD:(MBProgressHUD *)theHUD {
    [_HUD removeFromSuperview];
    _HUD = theHUD;
    [self.view addSubview:_HUD];
}

- (void)showEmptyImageView:(NSString *)imageName message:(NSString *)message{
    self.emptyLabel.text =  message;
    [self.emptyLabel sizeToFit];
    self.emptyLabel.bottom = kScreenHeight - 293 - 64;
    self.emptyLabel.centerX = kScreenWidth/2.f;
    self.emptyLabel.hidden = NO;
    
    UIImage *image = IMG(imageName);
    self.emptyImgView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    self.emptyImgView.image = image;
    self.emptyImgView.hidden = NO;
    self.emptyImgView.bottom = self.emptyLabel.top - 24;
    self.emptyImgView.centerX = kScreenWidth/2;
    self.isShowEmptyView = YES;
    
}

- (void)showEmptyViewWithMessage:(NSString *)message{
    self.emptyLabel.text =  message;
    [self.emptyLabel sizeToFit];
    self.emptyLabel.bottom =(kScreenHeight - kStatusBarHeight)/2 - 30.f;
    self.emptyLabel.centerX = kScreenWidth/2.f;
    self.emptyLabel.hidden = NO;
    self.emptyImgView.bottom  = self.emptyLabel.top - 35.f;
    self.emptyImgView.centerX = self.emptyLabel.centerX;
    self.emptyImgView.hidden = NO;
    self.isShowEmptyView = YES;
}
- (void)hideEmptyView{
    self.emptyLabel.text = @"";
    self.emptyLabel.hidden = YES;
    self.emptyImgView.hidden = YES;
    self.noNetWorkView.hidden = YES;
    self.isShowEmptyView = NO;
}

- (void)showNoNetWorkView{
    self.isShowNoNewworkView = YES;
    self.noNetWorkView.hidden = NO;
    self.noNetWorkView.centerX = kScreenWidth/2.f;
    self.noNetWorkView.bottom = (kScreenHeight - kStatusBarHeight)/2 - 30.f;
}

- (void)hideNoNetWorkView{
    self.isShowEmptyView = YES;
    self.noNetWorkView.hidden = YES;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _isViewWillAppear = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _isViewAppeared = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _isViewWillAppear = NO;
    _isViewAppeared = NO;
}

- (CGRect)rootViewFrame
{
    /* recalculate frame size */
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIApplication *application = [UIApplication sharedApplication];
    if (UIInterfaceOrientationIsLandscape(application.statusBarOrientation))
        size = CGSizeMake(size.height, size.width);
    if (!application.statusBarHidden)
        size.height -= MIN(application.statusBarFrame.size.width,
                           application.statusBarFrame.size.height);
    
    CGRect frame = self.view.frame;
    frame.size.height = size.height -
    self.navigationController.navigationBar.frame.size.height;
    return frame;
}
#pragma mark------device type
- (BOOL)isIPhone4{
    return kScreenHeight == 480 ? YES : NO;
}

- (BOOL)isIPhone5{
    return kScreenHeight == 568 ? YES : NO;
}

- (BOOL)isIPhone6{
    return kScreenHeight == 667 ? YES : NO;
}

- (BOOL)isIPhone6p{
    return kScreenHeight >= 736 ? YES : NO;
}

#pragma mark - Back Button

- (void)setEnableBackButton:(BOOL)enableBackButton {
    if (_enableBackButton != enableBackButton) {
        _enableBackButton = enableBackButton;
        
        if (!_enableBackButton) {
            self.navigationItem.leftBarButtonItem = nil;
            self.navigationItem.hidesBackButton = YES;
        } else {
            UIImage *normal = IMG(@"nav_icon_back");
            UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [leftButton setImage:normal forState:UIControlStateNormal];
            leftButton.frame = ccr(0, 0, 60, 44);
            if (IOS7_OR_LATER) {
                leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            }else{
                leftButton.width = 40.f;
            }
            [leftButton addTarget:self action:@selector(back:)
                 forControlEvents:UIControlEventTouchUpInside];
            [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, normal.size.width/2.f - 5.f, 0, 0)];
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        }
    }
}

- (void)back:(id)sender {
    if (self.enableBackButton && self.isViewAppeared) {
        if ([self.navigationController popViewControllerAnimated:YES] == nil) {
            self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

//- (void)search:(id)sender{
//    ICSearchViewController *vc = [[ICSearchViewController alloc] init];
//    [ICNavigator pushViewController:vc animated:YES];
//}
//
//- (void)setting:(id)sender{
//    if ([self.className isEqualToString:@"ICMeViewController"]) {
//        ICSettingViewController *settingV = [[ICSettingViewController alloc] init];
//        [ICNavigator pushViewController:settingV animated:YES];
//    }
//    
//}

- (UIView *)unReadView{
    if (!_unReadView) {
        _unReadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        _unReadView.backgroundColor = RGBCOLOR(0xfc5156);
        _unReadView.layer.cornerRadius = 2.5f;
        _unReadView.layer.masksToBounds = YES;
    }
    return _unReadView;
}

- (UIImageView *)emptyImgView{
    if (!_emptyImgView) {
        _emptyImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 65.f, 65.f)];
        _emptyImgView.image = IMG(@"无状态");
        _emptyImgView.hidden = YES;
    }
    return _emptyImgView;
}

- (UILabel *)emptyLabel{
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _emptyLabel.backgroundColor = [UIColor clearColor];
        _emptyLabel.font = [UIFont systemFontOfSize:12.f];
        _emptyLabel.textColor = RGBCOLOR(0x9D9AAA);
        _emptyLabel.hidden = YES;
    }
    return _emptyLabel;
}

- (UIView *)noNetWorkView{
    if (!_noNetWorkView) {
//        104*83
        _noNetWorkView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 104, 83 + 63.f)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 104, 83)];
        imageView.image = IMG(@"加载失败");
        [_noNetWorkView addSubview:imageView];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 104, 104,43)];
        label.font = [UIFont systemFontOfSize:12.f];
        label.textColor = RGBCOLOR(0x9D9AAA);
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"页面加载失败\n\n请点击页面刷新";
        [_noNetWorkView addSubview:label];
        _noNetWorkView.hidden = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickNoNetWorkView)];
        [_noNetWorkView addGestureRecognizer:tap];
    }
    return _noNetWorkView;
}
- (void)onClickNoNetWorkView{
    
}



@end

